package top.doe.spark

import scala.io.Source

object IteratorTest {

  def main(args: Array[String]): Unit = {

    val source = Source.fromFile("data/words.txt")

    //使用迭代器的方式读取数据
    val iter1: Iterator[String] = source.getLines()

    //将数据切分打平
    val iter2: Iterator[String] = iter1.flatMap(line => {
      val words = line.split(" ")
      words
    })

    //过滤
    val iter3: Iterator[String] = iter2.filter(word => {
      word.startsWith("h")
    })

    //将单词变成大写
    val iter4: Iterator[String] = iter3.map(word => {
      word.toUpperCase
    })

    while(iter4.hasNext) {
      val res = iter4.next()
      println(res)
    }

    source.close()

  }
}
